package com.masterclient.app.businessmodule.minemodule.cash.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.fragment.FCBaseFragment;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.common.config.BusinessConfig;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.minemodule.cash.adapter.BalanceGainAdapter;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceCashBean;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceGainBean;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceOrderBean;
import com.masterclient.app.businessmodule.minemodule.cash.contract.CashContract;
import com.masterclient.app.businessmodule.minemodule.cash.contract.CashPresenter;
import com.masterclient.app.businessmodule.minemodule.cash.detail.GainInfoActivity;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/10/12 15:16
 * 功能说明：
 */
public class BalanceGainFragment extends FCBaseFragment<CashPresenter> implements CashContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private BalanceGainAdapter balanceGainAdapter;

    private int total;

    @Override
    protected boolean useRefreshLayout() {
        return true;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.fragment_cash;
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        balanceGainAdapter = new BalanceGainAdapter();
        recyclerView.setAdapter(balanceGainAdapter);
        mPresenter.getBalanceGain("1", BusinessConfig.PAGE_SIZE,false);
        balanceGainAdapter.bindToRecyclerView(recyclerView);

        baseRefreshLayout().setOnRefreshListener(() -> {
            if (mPresenter.gainList != null) {
                mPresenter.gainList.clear();
                balanceGainAdapter.notifyDataSetChanged();
            }
            mPresenter.getBalanceGain("1", BusinessConfig.PAGE_SIZE,true);
        });

        balanceGainAdapter.setOnLoadMoreListener(() -> {
            if (mPresenter.gainList.size() < total) {
                mPresenter.getBalanceGain(String.valueOf(mPresenter.gainList.size() / Integer.valueOf(BusinessConfig.PAGE_SIZE) + 1), BusinessConfig.PAGE_SIZE,false);
            }
        }, recyclerView);
        balanceGainAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivitryWithBundle(GainInfoActivity.class, new String[][]{{"id", mPresenter.gainList.get(position).getSh_id()}, {"type", "1"}});
            }
        });
    }

    public void refreshList() {
        if (mPresenter.gainList != null) {
            mPresenter.gainList.clear();
            balanceGainAdapter.notifyDataSetChanged();
        }
        if (mPresenter != null)
            mPresenter.getBalanceGain("1", BusinessConfig.PAGE_SIZE,false);
    }

    @Override
    public void showBalanceGain(BalanceGainBean.DataBean bean) {

        if (baseRefreshLayout() != null && baseRefreshLayout().isRefreshing()) {
            baseRefreshLayout().setRefreshing(false);
        }

        total = Integer.valueOf(bean.getSh_total_rows());
        if (mPresenter.gainList == null) {
            mPresenter.gainList = new ArrayList<>();
        }

        mPresenter.gainList.addAll(bean.getSh_items());
        balanceGainAdapter.setNewData(mPresenter.gainList);

        if (mPresenter.gainList.size() >= total) {
            balanceGainAdapter.setEnableLoadMore(false);
        }


        if (dataListerner != null) {
            dataListerner.setData(bean, bean.getSh_balance_info());
            dataListerner.setIntro(bean.getSh_balance_introduce(), bean.getSh_is_bind_mobile() + "");
        }
    }

    @Override
    public void showBalanceOrder(BalanceOrderBean.DataBean bean) {
    }

    @Override
    public void showBalanceCash(BalanceCashBean.DataBean bean) {
    }

    @Override
    public void loadFailed() {
        if (baseRefreshLayout() != null && baseRefreshLayout().isRefreshing()) {
            baseRefreshLayout().setRefreshing(false);
        }
        if (mPresenter.gainList!=null && (mPresenter.gainList.size()/Integer.valueOf(BusinessConfig.PAGE_SIZE)+1) > 1) {
            balanceGainAdapter.loadMoreFail();
        }
    }

    public interface DataListerner {
        void setData(BalanceGainBean.DataBean dataBean, BalanceGainBean.DataBean.ShBalanceInfoBean balanceInfoBean);

        void setIntro(String url, String sh_is_bind_mobile);
    }

    private DataListerner dataListerner;

    public void setDataListerner(DataListerner dataListerner) {
        this.dataListerner = dataListerner;
    }
}
